import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isUndefined from "lodash/isUndefined";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2;

/**
 * PlatformManager - 平台管理员
 * @date: 2019-01-10
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import ExcelExport from 'components/ExcelExport';
import { Content, Header } from 'components/Page';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { getCurrentOrganizationId, filterNullValueObject } from 'utils/utils';
import { DEFAULT_DATETIME_FORMAT } from 'utils/constants';
import formatterCollections from 'utils/intl/formatterCollections';
import FilterForm from "./FilterForm";
import ListTable from "./ListTable";
import ClearLogsDrawer from "./ClearLogsDrawer";
var PlatformManager = (_dec = connect(function (_ref) {
  var platformManager = _ref.platformManager,
      loading = _ref.loading;
  return {
    platformManager: platformManager,
    clearLogLoading: loading.effects['platformManager/clearLogs'],
    loading: loading.effects['platformManager/fetchMembers'],
    tenantId: getCurrentOrganizationId()
  };
}), _dec2 = formatterCollections({
  code: ['cpfm.login']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(PlatformManager, _PureComponent);

  var _super = _createSuper(PlatformManager);

  function PlatformManager(props) {
    var _this;

    _classCallCheck(this, PlatformManager);

    _this = _super.call(this, props);
    _this.state = {
      clearLogsVisible: false // 清除日志模态框是否可见

    };
    return _this;
  }

  _createClass(PlatformManager, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var dispatch = this.props.dispatch;
      var lovCodes = {
        typeList: 'CPFM.LOGIN_AUDIT_TYPE'
      };
      dispatch({
        type: 'platformManager/init',
        payload: {
          lovCodes: lovCodes
        }
      });
      var type = 'platformManager/fetchMembers';
      dispatch({
        type: type
      });
    }
    /**
     * form元素绑定
     * @param {*} [ref={}]
     * @memberof PlatformManager
     */

  }, {
    key: "handleBindRef",
    value: function handleBindRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.form = (ref.props || {}).form;
    }
    /**
     * 条件查询及表格翻页
     * @param {*} [fields={}]
     * @memberof PlatformManager
     */

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      var filterValues = this.handleGetFormValue();
      dispatch({
        type: 'platformManager/fetchMembers',
        payload: _objectSpread({
          page: _isEmpty(fields) ? {} : fields
        }, filterValues)
      });
    }
    /**
     * 获取form数据
     */

  }, {
    key: "handleGetFormValue",
    value: function handleGetFormValue() {
      var formValue = _isUndefined(this.form) ? {} : this.form.getFieldsValue();

      var values = _objectSpread(_objectSpread({}, formValue), {}, {
        loginDateAfter: formValue.loginDateAfter && formValue.loginDateAfter.format(DEFAULT_DATETIME_FORMAT),
        loginDateBefore: formValue.loginDateBefore && formValue.loginDateBefore.format(DEFAULT_DATETIME_FORMAT)
      });

      var filterValues = filterNullValueObject(values);
      return filterValues;
    }
    /**
     * 日志清理
     * @param {object} fieldsValue - 请求参数
     */

  }, {
    key: "handleClearLogs",
    value: function handleClearLogs(fieldsValue) {
      var _this2 = this;

      var dispatch = this.props.dispatch;
      dispatch({
        type: 'platformManager/clearLogs',
        payload: fieldsValue
      }).then(function (res) {
        if (res) {
          notification.success();

          _this2.handleClearLogsDrawer(false);

          _this2.handleSearch();
        }
      });
    }
    /**
     *是否打开清除日志模态框
     *
     * @param {boolean} flag
     */

  }, {
    key: "handleClearLogsDrawer",
    value: function handleClearLogsDrawer(flag) {
      this.setState({
        clearLogsVisible: flag
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props = this.props,
          loading = _this$props.loading,
          path = _this$props.match.path,
          _this$props$clearLogL = _this$props.clearLogLoading,
          clearLogLoading = _this$props$clearLogL === void 0 ? false : _this$props$clearLogL,
          _this$props$platformM = _this$props.platformManager,
          _this$props$platformM2 = _this$props$platformM.list,
          list = _this$props$platformM2 === void 0 ? [] : _this$props$platformM2,
          _this$props$platformM3 = _this$props$platformM.pagination,
          pagination = _this$props$platformM3 === void 0 ? {} : _this$props$platformM3,
          _this$props$platformM4 = _this$props$platformM.typeList,
          typeList = _this$props$platformM4 === void 0 ? [] : _this$props$platformM4,
          _this$props$platformM5 = _this$props$platformM.clearTypeList,
          clearTypeList = _this$props$platformM5 === void 0 ? [] : _this$props$platformM5;
      var clearLogsVisible = this.state.clearLogsVisible;
      var filterProps = {
        onRef: this.handleBindRef,
        onSearch: this.handleSearch,
        typeList: typeList
      };
      var listProps = {
        loading: loading,
        pagination: pagination,
        dataSource: list,
        onChange: this.handleSearch
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cpfm.login.view.message.title').d('用户登录日志')
      }, /*#__PURE__*/React.createElement(ExcelExport, {
        exportAsync: true,
        requestUrl: "/cpfm/v1/audit-logins/export",
        otherButtonProps: {
          type: 'primary'
        },
        queryParams: this.handleGetFormValue()
      }), /*#__PURE__*/React.createElement(ButtonPermission, {
        icon: "delete",
        onClick: function onClick() {
          return _this3.handleClearLogsDrawer(true);
        },
        permissionList: [{
          code: "".concat(path, ".button.clearLogs"),
          type: 'button',
          meaning: '登录日志-日志清理'
        }]
      }, intl.get('cpfm.login.view.button.clearLogs').d('日志清理'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(FilterForm, filterProps), /*#__PURE__*/React.createElement(ListTable, listProps), /*#__PURE__*/React.createElement(ClearLogsDrawer, {
        title: intl.get('cpfm.login.view.button.clearLogs').d('日志清理'),
        loading: clearLogLoading,
        modalVisible: clearLogsVisible,
        clearTypeList: clearTypeList,
        onCancel: function onCancel() {
          return _this3.handleClearLogsDrawer(false);
        },
        onOk: this.handleClearLogs
      })));
    }
  }]);

  return PlatformManager;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleBindRef", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleBindRef"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleGetFormValue", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleGetFormValue"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleClearLogs", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleClearLogs"), _class2.prototype)), _class2)) || _class) || _class);
export { PlatformManager as default };